package com.wangmeng.book.job;

import com.wangmeng.book.dao.BaseDaoSupport;
import com.wangmeng.book.entity.BookDoc;
import com.wangmeng.book.uploader.WebBookPublisher;
import com.wangmeng.book.utils.ConstValueUtils;
import com.wangmeng.book.utils.SpringContextManager;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * User: 王猛
 * Date: 13-9-29
 * 书籍发布任务
 */
public class BookPublishJob implements Runnable {
    private static final Logger logger = Logger.getLogger(BookPublishJob.class);

    public void run() {
        logger.info("内容发布检查开始");
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            WebBookPublisher wbp = new WebBookPublisher(ConstValueUtils.WEB_USERNAME, ConstValueUtils.WEB_PASSWORD);
            if (wbp.login()) {
                logger.info("发布新记录");
                List<BookDoc> bookList = daoSupport.listEntityByCondition(BookDoc.class, " and approveStatus = ? ", new Object[]{ConstValueUtils.APPROVE_PUBLISH});
                for (BookDoc bd : bookList) {
                    wbp.publish(bd);
                }
                logger.info("更新已有记录");
                bookList = daoSupport.listEntityByCondition(BookDoc.class, " and approveStatus = ? ", new Object[]{ConstValueUtils.APPROVE_HAS_UPDATE});
                for (BookDoc bd : bookList) {
                    wbp.update(bd);
                }
            } else {
                logger.info("登陆失败");
            }
        } catch (Exception ex) {
            logger.error("内容发布检查异常", ex);
        } finally {
            logger.info("内容发布检查结束");
        }
    }
}
